package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.entity.DeviceData;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;


    @Override
    public PageResponse<DeviceDataVo> getPage(DeviceDataPageReqDto dto) {
        Long startTime = dto.getStartTime();
        Long endTime = dto.getEndTime();
        if(startTime!=null && endTime!=null) {
            // 把Long类型的时间处理成LocalDateTime
            LocalDateTime startDate = LocalDateTimeUtil.of(startTime);
            LocalDateTime endDate = LocalDateTimeUtil.of(endTime);
            dto.setStartDate(startDate);
            dto.setEndDate(endDate);
        }

        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page<DeviceDataVo> voPage = (Page<DeviceDataVo>)  deviceDataMapper.selectList(dto);

        return PageResponse.of(voPage, DeviceDataVo.class);
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {

        List<DeviceDataGraphVo> deviceDataGraphVoList = deviceDataMapper.queryDeviceDataListByDay(iotId, functionId, LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));

        // 把deviceDataGraphVoList通过Stream转成Map
        Map<String, DeviceDataGraphVo> map = deviceDataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, deviceDataGraphVo -> deviceDataGraphVo));
        // 00:00--DeviceDataGraphVo
        // 01:00--DeviceDataGraphVo

        for (int i = 0; i < 24; i++) {
            // 需要使用字符串补全方法、00  01 02 03 04 5 6 7 8 9 10 11
            String dateTime = String.format("%02d",i)+":00";
            System.out.println(dateTime);

            DeviceDataGraphVo deviceDataGraphVo = map.get(dateTime);
            if(deviceDataGraphVo==null){
                deviceDataGraphVo = new DeviceDataGraphVo();
                deviceDataGraphVo.setDateTime(dateTime);
                deviceDataGraphVo.setDataValue(0.0);

                deviceDataGraphVoList.add(i,deviceDataGraphVo);
            }

        }

        return deviceDataGraphVoList;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {

        List<DeviceDataGraphVo> deviceDataGraphVoList = deviceDataMapper.queryDeviceDataListByWeek(iotId, functionId, LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));

        // 把deviceDataGraphVoList通过Stream转成Map
        Map<String, DeviceDataGraphVo> map = deviceDataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, deviceDataGraphVo -> deviceDataGraphVo));
        // 00:00--DeviceDataGraphVo
        // 01:00--DeviceDataGraphVo
        // startTime++

        for (int i = 0; i < 7; i++) {  //08月08日   yyyy-MM-dd HH:mm:ss
            // 需要使用字符串补全方法、00  01 02 03 04 5 6 7 8 9 10 11
            String dateTime = LocalDateTimeUtil.format(LocalDateTimeUtil.of(startTime).plusDays(i), "MM月dd日");
            System.out.println(dateTime);

            DeviceDataGraphVo deviceDataGraphVo = map.get(dateTime);
            if(deviceDataGraphVo==null){
                deviceDataGraphVo = new DeviceDataGraphVo();
                deviceDataGraphVo.setDateTime(dateTime);
                deviceDataGraphVo.setDataValue(0.0);

                deviceDataGraphVoList.add(i,deviceDataGraphVo);
            }

        }

        return deviceDataGraphVoList;
    }
}
